<script type="text/html" template>
<div class="layui-fluid" >
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="editForm">
                <input type="hidden" id="userId" name="id"  value="{{d.id}}" />
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="required" value="{{d.username}}" autocomplete="off" placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="realName" lay-verify="required" value="{{d.realName}}"  placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block" id="roleList">

                    </div>
                </div>
                <input type="hidden" name="status" id="status"  value="{{d.status}}" />
                <div class="layui-form-item">
                    <label class="layui-form-label">状态-默认开</label>
                    <div class="layui-input-block">
                        <input type="checkbox" {{# if(d.status == 1){ }} checked="" {{# } }} lay-skin="switch" lay-filter="status" lay-text="正常|禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <a class="layui-btn" lay-submit="" lay-filter="editUserSubmit">立即提交</a>
                        <a class="layui-btn layui-btn-primary" id="close" >关闭</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</script>
<script>
    var EditUser = function (index) {
        var admin = layui.admin,
            form = layui.form,
            $ =layui.jquery;
        //初始化
        this.init = function () {
            var roleNumber = 0;
            //获取当前角色
            admin.get({
                api:'getCurrentRoleList',
                urlStr: "/"+$("#userId").val(),
                success:function (resp) {
                    if(resp.data) {
                        var roleMap = {};
                        resp.data.roleIdList.forEach(function (str) {
                            roleMap[str] = true;
                        });
                        //初始化角色
                        admin.get({
                            api:'getRoleList',
                            success:function (res) {
                                if(res.data) {
                                    roleNumber = res.data.length;
                                    var roleListHtml = [];
                                    res.data.forEach(function (obj, index) {
                                        if(roleMap[obj.roleId]){
                                            roleListHtml.push('<input type="checkbox" checked name="roleIdList['+index+']" value="'+obj.roleId+'" title="'+obj.roleName+'">')
                                        }else{
                                            roleListHtml.push('<input type="checkbox"  name="roleIdList['+index+']" value="'+obj.roleId+'" title="'+obj.roleName+'">')
                                        }
                                    });
                                    $("#roleList").html(roleListHtml.join(""));
                                    form.render(null, 'editForm');
                                }
                            }
                        });
                    }
                }
            });
            /* 监听指定开关 */
            form.on('switch(status)', function (data) {
               if(this.checked){
                    $("#status").val(1);
               }else{
                   $("#status").val(0);
               }
            });

            /* 监听提交 */
            form.on('submit(editUserSubmit)', function (data) {
                var requestData = data.field;
                var roleIdList = [];
                for(var i=0;i< roleNumber;i++){
                    if(data.field['roleIdList['+i+']']){
                        roleIdList.push(data.field['roleIdList['+i+']']);
                        delete requestData['roleIdList['+i+']'];
                    }
                }
                requestData.roleIdList = roleIdList;
                //提交用户
                admin.post({
                    api:'updateUser',
                    data: JSON.stringify(requestData),
                    contentType: 'application/json;charset=UTF-8',
                    success:function (res) {
                        if(res.code == 0){
                            admin.success('操作成功',function () {
                                parent.userList.refresh();
                                parent.layer.close(index);
                            });
                        }
                    }
                });
                return false;
            });
            //关闭窗口
            $("#close").click(function () {
                parent.layer.close(index);
            });
        };
    };

</script>